const TokenKey = 'token'
const TokenExpireKey = 'token_expire'

export function getToken() {
  const token = localStorage.getItem(TokenKey)
  const expireTime = localStorage.getItem(TokenExpireKey)

  if (!token || !expireTime || Date.now() > parseInt(expireTime)) {
    removeToken()
    return null
  }

  return token
}

export function setToken(token) {
  const expireTime = Date.now() + 24 * 60 * 60 * 1000 // 24小时
  localStorage.setItem(TokenKey, token)
  localStorage.setItem(TokenExpireKey, expireTime.toString())
}

export function removeToken() {
  localStorage.removeItem(TokenKey)
  localStorage.removeItem(TokenExpireKey)
}

export function getUserInfo() {
  const userInfo = localStorage.getItem('userInfo')
  return userInfo ? JSON.parse(userInfo) : null
}

export function setUserInfo(userInfo) {
  return localStorage.setItem('userInfo', JSON.stringify(userInfo))
}

export function removeUserInfo() {
  return localStorage.removeItem('userInfo')
}
